package cn.com.ylpw.web.order.service.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import cn.com.ylpw.web.order.service.payService;
import cn.com.ylpw.web.util.AbstractService;
import cn.com.ylpw.web.util.RestTemplateUtil;
import cn.com.ylpw.web.util.StringUtil;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;

@Service("payService")
public class payServiceImpl extends AbstractService  implements payService {
	 public final Logger log = LoggerFactory.getLogger(getClass());
	
	private final static String NAME_SPACE = "cn.com.ylpw.web.order.Pay";
	public static final String PAY_URL="http://pay.228/pay/getCashier.do"; //支付接口
	public static final String TO_SIGN="http://pay.228/pay/SignFastPay.do"; //签约(浦发银行)
	public static final String GET_CODE="http://pay.228/pay/getVerifyCode.do"; //获取验证码(浦发银行)
	public static final String GET_CARDNO="http://pay.228/pay/getFastPay.do"; //获取绑定的浦发银行卡号
	@Override
	/**
	 * 
	 * @description 向第三方接口发送xml数据请求
	 * @param xmlData
	 * @return
	 */
	public Map<String,Object> payClientPost(Map<String,Object> paramMap,String clinentUrl) {
		
		RestTemplate restTemplate = RestTemplateUtil.getInstance();
		HttpHeaders requestHeaders = new HttpHeaders();
		LinkedMultiValueMap<String,Object> vars = new LinkedMultiValueMap<String, Object>();
		for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
			 vars.add(entry.getKey(),  entry.getValue().toString());
	    }
		
		HttpEntity<Object> entity = new HttpEntity<Object>(vars, requestHeaders);
		int result;
		String repXml="";
		HashMap<String,Object> repMap = Maps.newHashMap();
		try {
			ResponseEntity<String> responseEntity = restTemplate.postForEntity(clinentUrl, entity, String.class);
			result = responseEntity.getStatusCode().value();
			repXml = responseEntity.getBody();
			repMap.put("result", result);
		    repMap.put("jsonData", repXml);
		} catch (Exception e) {
			log.error("向第三方接口发送xml数据请求失败{}",paramMap, e);
		}
        return repMap;
	}
	@Override
	public Map<String, Object> getpayInfoMap(Map<String, Object> paramMap) {
		paramMap.put("channel_code", "web");
		Map<String,Object> retMap = this.payClientPost(paramMap,PAY_URL);
		Integer result = (Integer)retMap.get("result");
		if(result != null && result == 200){
			String retJson = (String)retMap.get("jsonData");
			ObjectMapper mapper = new ObjectMapper();
			try {
				Map<String,Object> map= mapper.readValue(retJson, Map.class);
				if (map!= null && map.size()>0) {
					return map;
				}else {
					return null;
				}
			} catch (Exception e) {
				log.error("获取支付列表失败{}",paramMap, e);
				return null;
			}
		}else{
			return null;
		}
	}
	@Override
	public List<Map<String, Object>> findOrderPayById(Map<String, Object> params) {
		return super.superDao.getMyBatisFactory().findObjectList(NAME_SPACE, "findOrderPayById", params);
	}
	@Override
	public List<Map<String, Object>> findOrderPublicPayStatus(Map<String, Object> params) {
		return super.superDao.getMyBatisFactory().findObjectList(NAME_SPACE, "findOrderPublicPayStatus", params);
	}
	@Override
	public String toSignSPDB(Map<String, Object> paramMap) {
		paramMap.put("branch_name", "bj");
		paramMap.put("channel_code", "web");
		paramMap.put("yfb_pay_code", "400");
		Map<String,Object> retMap = this.payClientPost(paramMap,TO_SIGN);
		Integer result = (Integer)retMap.get("result");
		if(result != null && result == 200){
			String retJson = (String)retMap.get("jsonData");
			if (!StringUtil.isEmpty(retJson)) {
				return retJson;
			}
		}else{
			return null;
		}
		return null;
	}
	@Override
	public String getCodeSPDB(Map<String, Object> paramMap) {
		paramMap.put("branch_name", "bj");
		paramMap.put("channel_code", "web");
		paramMap.put("yfb_pay_code", "400");
		Map<String,Object> retMap = this.payClientPost(paramMap,GET_CODE);
		Integer result = (Integer)retMap.get("result");
		if(result != null && result == 200){
			String retJson = (String)retMap.get("jsonData");
			if (!StringUtil.isEmpty(retJson)) {
				return retJson;
			}
		}else{
			return null;
		}
		return null;
	}
	@Override
	public String getCardno(Map<String, Object> paramMap) {
		paramMap.put("branch_name", "bj");
		paramMap.put("channel_code", "web");
		paramMap.put("yfb_pay_code", "400");
		Map<String,Object> retMap = this.payClientPost(paramMap,GET_CARDNO);
		Integer result = (Integer)retMap.get("result");
		if(result != null && result == 200){
			String retJson = (String)retMap.get("jsonData");
			if (!StringUtil.isEmpty(retJson)) {
				return retJson;
			}
		}else{
			return null;
		}
		return null;
	}

}
